package imse.team0;

import imse.team0.scalability.configuration.RouteConfigurationProperties;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.component.jms.JmsEndpoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Eugen, 5/6/12 4:22 PM
 */
public class CamelRouteConfigurator implements CamelContextAware {

    private static final String prop_ORDER_ID_FILTER = "orderIDFilter";
    private static final Logger LOG = LoggerFactory.getLogger(CamelRouteConfigurator.class);
    private static RouteConfigurationProperties props;

    public void setRouteDynamicConfigurations(RouteConfigurationProperties props) throws Exception {
        this.props = props;
    }

    public void setCamelContext(CamelContext camelContext) {
        String paymentConfirmationSelector = props.getProperty(prop_ORDER_ID_FILTER);
        if (paymentConfirmationSelector == null) {
            LOG.error("-------------> Could not retrieve selector value from RouteConfigurationProperties object");
            throw new RuntimeException("Route Configuration Properties are not available");
        }
        try {
            camelContext.stopRoute("Route_2_2_process_invoice_payment");
            JmsEndpoint endpoint = camelContext.getEndpoint("jms:invoicePayment", JmsEndpoint.class);
            endpoint.setSelector("invoiceGroup='" + paymentConfirmationSelector + "'");
            camelContext.startRoute("Route_2_2_process_invoice_payment");
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        props = null; // don't need the properties anymore
    }

    public CamelContext getCamelContext() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
